Page({

  /**
   * 页面的初始数据
   */
  data: {
    istokenPop: false,//判断是否等了
    // tabClick: 1,


    params: {
      ageType:1,
      page: 1,
      size: 10
    },

    list: []
  },



  loginCode() {

    var _this = this
    this.setData({
      istokenPop: false
    })

    // wx.navigateTo({
    //   url: '/package/juanshu/pages/buyBook/buyBook?wasActivities=1',
    // })

  },


  clickTab (e) {
    // console.log(e)

    var index = e.currentTarget.dataset.tabindex

    var ageType = "params.ageType"
    var page = "params.page"
    var size = "params.size"

    this.setData({
      [ageType]: index,
      [page]: 1,
      [size]: 10,
      list: []
    })


    this.getList(this.data.params)

   

    // this.setData({
    //   tabClick: index
    // })
  },

  addCards (e) {
  
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    

    var bookInfoId = e.currentTarget.dataset.bookid

    console.log(bookInfoId)

    this.getAddapi(bookInfoId)

  },

  goShopcar () {
    wx.navigateTo({
      url: '/package/my/pages/shopCar/shopCar',
    })
  },


  getAddapi (id) {


    var _this = this

    var params = {
      bookInfoId: id
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findMyTrolleyBook/auth',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res)=>{
        if (res.data.code == 200) {
          if (res.data.data.wasTrolleyBook==true) {
            wx.showToast({
              title: '您已添加过这本书',
              icon: 'none'
            })

            return
          }else{
            _this.addQuery(id)
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })

  },

  addQuery(bookid){

    var params = {
      userId: wx.getStorageSync('userId'),
      bookInfoId:bookid,
      bookQuality:3,
      shopScores:0
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxShoppingTrolleys/mod/auth',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res)=>{
        if (res.data.code==200) {
          wx.showToast({
            title: '添加成功',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
  },



  getList (params) {

    var _this = this
    wx.showLoading({
      title: '加载中'
    })
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/book/findActiveBook',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
      },
      success:(res)=>{
        if (res.data.code == 200) {
          wx.hideLoading()


          if (res.data.data.length==0) {
            wx.showToast({
              title: '加载完毕',
              icon:'none'
            })


            return
          }

          var list = _this.data.list


          for (var i = 0;i< res.data.data.length;i++) {
            list.push(res.data.data[i])
          }

          _this.setData({
            list: list
          })

        }else{
          wx.hideLoading()
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  loadMore () {
    
    this.data.params.page += 1

    this.getList(this.data.params)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(this.data.params)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})